<template>
  <div class="detail">
    <el-card class="mycard">
      <div class="print">
        <el-button class="btn" size="mini" type="primary" @click="toprint">
          打印
        </el-button>
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录帐户设置" name="first">
          <el-form :model="form" label-width="100px" class="myform">
            <el-form-item label="姓名">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" />
            </el-form-item>
            <el-form-item class="myformitem">
              <el-button type="primary" @click="updateUserInfo">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userinfo />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">角色管理</el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import userinfo from '../components/userinfo.vue'
import { getUserInfoById, updateUserInfoById } from '@/api/employees'
export default {
  components: {
    userinfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      info: {}
    }
  },
  created() {
    // console.log(this.$route.params.id)
    this.getUserInfoById()
    // console.log(this.form)
  },
  methods: {
    toprint() {
      this.$router.push(
        `/employees/print/${this.userId}?type=${this.activeName}`
      )
    },
    async getUserInfoById() {
      const res = await getUserInfoById(this.userId)
      // console.log(res)
      this.form.username = res.username
      this.info = res
    },
    async updateUserInfo() {
      try {
        await updateUserInfoById({ ...this.info, ...this.form })
        // console.log(res)
        this.$message.success('更新成功')
        // this.getUserInfoById()
        this.$router.push('/employees')
      } catch (err) {
        this.$message.error(err.message)
      }
    }
  }
}
</script>

<style lang="scss" scopend>
.detail {
  padding: 20px;
  .mycard {
    .print {
      position: relative;
      .btn {
        position: absolute;
        right: 8px;
        top: 0;
        z-index: 999;
      }
    }
    .myform {
      width: 400px;
      margin: 30px auto;
      .myformitem {
        text-align: center;
      }
    }
  }
}
</style>
